package com.dykj.fireFighting.web.rocketmq.consumer;

import com.dykj.fireFighting.api.base.appNotification.PushModel;
import com.dykj.fireFighting.api.base.push.service.PushService;
import com.dykj.fireFighting.common.core.constant.CommonConstant;
import com.dykj.fireFighting.common.core.constant.WebsocketConstant;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.base.websocket.entity.MoreMessage;
import com.dykj.fireFighting.web.rocketmq.message.FirePushMessage;
import com.dykj.fireFighting.web.utils.SpringUtil;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhengbo
 */
@RocketMQMessageListener(
        topic = FirePushMessage.TOPIC,
        consumerGroup = "dy-consumer-group-" + FirePushMessage.TOPIC
)
public class FirePushConsumer implements RocketMQListener<FirePushMessage> {
    public static final Logger LOGGER = LoggerFactory.getLogger(FirePushConsumer.class);
    private static final PushService pushService= SpringUtil.getBean("pushService");
    private static RedisService redisService= SpringUtil.getBean(RedisService.class);
    @Override
    public void onMessage(FirePushMessage firePushMessage) {
        System.out.println("=======接收到火警推送消息=======");
        String time = firePushMessage.getAlarmTime().substring(11);
        PushModel pushModel=new PushModel();
        MoreMessage moreMessage=new MoreMessage();
        moreMessage.setMsgId(firePushMessage.getDevHisId());
        //火警
        if ("08".equals(firePushMessage.getKey())){
            if (firePushMessage.getIsAppPush()){
                //APP推送
                pushModel.setTitle("火警通知");
                pushModel.setBody("贵单位"+firePushMessage.getUnit().getUnitName()+firePushMessage.getDeviceName()+"于今天"+time+"发生警情，请确认");
                pushModel.setAlias(firePushMessage.getUnit().getUnitId());
                pushModel.setClickType(CommonConstant.APPCLICKTYPE);
                boolean b = pushService.pushAlias(pushModel);
                System.out.println("================APP推送成功==================");
            }
            //websocket推送
            moreMessage.setMsgTitle("火警通知");
            moreMessage.setCmd(WebsocketConstant.CMD_FIRE);
            moreMessage.setMsgTxt("您有一条来自" + firePushMessage.getDeviceName() + "的火警通知，点击查看！");
            moreMessage.setUserIds(firePushMessage.getUnit().getUnitUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
            moreMessage.setMsgTxt("您有一条来自" +firePushMessage.getUnit().getUnitName()+"的"+ firePushMessage.getDeviceName() + "的火警通知，点击查看！");
            moreMessage.setUserIds(firePushMessage.getUnit().getCenterUserIds().toArray(new String[0]));
            moreMessage.setUnitId(firePushMessage.getUnit().getUnitId());
            redisService.convertAndSend("channel:moreMessage",moreMessage);

            moreMessage.setMsgTxt("您有一条来自" +firePushMessage.getUnit().getUnitName()+"的"+ firePushMessage.getDeviceName() + "的火警通知，点击查看！");
            moreMessage.setUserIds(firePushMessage.getUnit().getUnitIds().toArray(new String[0]));
            moreMessage.setUnitId(firePushMessage.getUnit().getUnitId());
            redisService.convertAndSend("channel:moreMessage",moreMessage);
            System.out.println("================WebSocket推送成功==================");
        }
        //故障
        if ("07".equals(firePushMessage.getKey())){
            if (firePushMessage.getIsAppPush()){
                //APP推送
                pushModel.setTitle("故障通知");
                pushModel.setBody("贵单位"+firePushMessage.getUnit().getUnitName()+firePushMessage.getDeviceName()+"于今天"+time+"发生故障，请确认");
                pushModel.setAlias(firePushMessage.getUnit().getUnitId());
                pushModel.setClickType(CommonConstant.APPCLICKTYPE);
                boolean b = pushService.pushAlias(pushModel);
                System.out.println("================APP推送成功==================");
            }
            //websocket推送
            moreMessage.setMsgTitle("故障通知");
            moreMessage.setCmd(WebsocketConstant.CMD_TOPIC);
            moreMessage.setMsgTxt("您有一条来自" + firePushMessage.getDeviceName() + "的故障通知，点击查看！");
            moreMessage.setUserIds(firePushMessage.getUnit().getUnitUserIds().toArray(new String[0]));
            redisService.convertAndSend("channel:moreMessage",moreMessage);
            moreMessage.setMsgTxt("您有一条来自" +firePushMessage.getUnit().getUnitName()+"的"+ firePushMessage.getDeviceName() + "的故障通知，点击查看！");
            moreMessage.setUserIds(firePushMessage.getUnit().getCenterUserIds().toArray(new String[0]));
            moreMessage.setUnitId(firePushMessage.getUnit().getUnitId());
            redisService.convertAndSend("channel:moreMessage",moreMessage);
            System.out.println("================WebSocket推送成功==================");
        }
    }
}
